package com.shell.customer.accreditation.rx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.customer.accreditation.AccreditEvent;
import com.shell.customer.accreditation.vo.AccreditationVo;
import com.shell.customer.base.service.CusExtraService;
import com.shell.customer.base.service.CusRecService;
import com.shell.customer.invitation.service.InvtCusService;
import com.shell.customer.po.CusExtra;
import com.shell.customer.po.CusInvt;
import com.shell.customer.po.CusRec;
import com.shell.feign.PubUserExtraFeign;
import com.shell.feign.PubUserFeign;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.user.po.PubUser;
import com.shell.governor.user.po.PubUserExtra;

@Component
public class SetInviterSubscriber extends OnErrorNotImplementedSubscriber<AccreditEvent> {
	@Autowired
	private CusRecService customerCService;
	@Autowired
	private InvtCusService invtCusService;
	@Autowired
	private CusExtraService pubCusExtraService;
	@Autowired
	private PubUserFeign userFeign;
	@Autowired
	private PubUserExtraFeign userExtraFeign;

	public void onApplicationEvent(AccreditEvent event) {
		AccreditationVo accreditationVo = event.getAccreditationVo();
		CusRec cus = accreditationVo.getCustomerC();
		if (StringUtil.notEmpty(accreditationVo.getInviteCode())) {
			PubUserExtra userExtra = new PubUserExtra();
			userExtra.setInviteCode(accreditationVo.getInviteCode());
			userExtra = this.userExtraFeign.selectOne(userExtra);
			PubUser inviterUser = this.userFeign.selectByKey(userExtra.getUserId());

			CusExtra cusExtra = (CusExtra) this.pubCusExtraService.selectByKey(cus.getCusId());
			if (null == cusExtra) {
				cusExtra = new CusExtra();
				cusExtra.setInviterId(userExtra.getUserId());
				cusExtra.setInviterCusId(userExtra.getEntId());
				cusExtra.setInviterName(inviterUser.getUserName());
				cusExtra.setInviterPhone(inviterUser.getPhoneNo());
				cusExtra.setCusId(cus.getCusId());
				this.pubCusExtraService.save(cusExtra);
			} else {
				cusExtra.setInviterId(userExtra.getUserId());
				cusExtra.setInviterCusId(userExtra.getEntId());
				cusExtra.setInviterName(inviterUser.getUserName());
				cusExtra.setInviterPhone(inviterUser.getPhoneNo());
				this.pubCusExtraService.updateNotNull(cusExtra);
			}
			CusRec inviter = (CusRec) this.customerCService.selectByKey(userExtra.getEntId());
			CusInvt cusInvt = new CusInvt();
			cusInvt.setInviterId(inviter.getCusId());
			cusInvt.setInviteeName(cus.getCusName());
			cusInvt = (CusInvt) this.invtCusService.selectOne(cusInvt);
			if (cusInvt == null) {
				CusInvt invt = new CusInvt();
				invt.setInvtId(SystemUtil.getUUID());
				invt.setInviterId(inviter.getCusId());
				invt.setInviteeId(cus.getCusId());
				invt.setInviteeName(cus.getCusName());
				invt.setInviteeCusType("SUPPLIER");
				invt.setContactorName(cus.getContactorName());
				invt.setContactorPhone(cus.getContactorPhone());
				invt.setInviterTimes(Integer.valueOf(1));
				invt.setEditorId(CommonDataUtil.getUserId());
				invt.setEditTime(DateUtil.getSystemDateTime());
				invt.setCreateTime(DateUtil.getSystemDateTime());
				this.invtCusService.save(invt);
			} else {
				cusInvt.setInviterTimes(Integer.valueOf(cusInvt.getInviterTimes().intValue() + 1));
				cusInvt.setInviteeId(cus.getCusId());
				this.invtCusService.updateAll(cusInvt);
			}
		}
	}
}
